/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.main.controller;

import com.kimstore.gui.main.Inventory;
import com.kimstore.gui.stocksarrival.NewStocksController;
import com.kimstore.bean.Batch;
import com.kimstore.bean.Item;
import com.kimstore.bean.Product;
import com.kimstore.factory.DaoFactory;
import com.kimstore.gui.stocksarrival.ViewBatchController;
import com.kimstore.utilities.CustomTableRowSorter;
import com.kimstore.utilities.DateUtil;
import com.kimstore.utilities.PaginateSearchAndFilter;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.decorator.HighlighterFactory;

/**
 *
 * @author Administrator
 */
public class StocksArrivalTabController extends BaseTabController {

    public Inventory parentController;
    private PaginateSearchAndFilter<Batch> psaf;

    public StocksArrivalTabController(Inventory parentController) {
        this.parentController = parentController;
    }

    @Override
    public void prepareUserInterface() {
        table.setShowGrid(true, true);
        table.addHighlighter(HighlighterFactory.createComputedForegroundHighlighter());
    }

    public void populateStocksArrivalTable() {
        populateStocksArrivalTable(psaf.search());
    }

    private void populateStocksArrivalTable(List<Batch> batches) {
        if (batches != null) {
            DefaultTableModel model = (DefaultTableModel) table.getModel();
            model.setRowCount(0);
            for (Batch b : batches) {
                model.insertRow(model.getRowCount(),
                        new Object[]{b.getNumber(),
                            DateUtil.formatDate(b.getDateReceived()),
                            b.getReceivedBy(),
                            getBatchItems(b.getItems()),
                            b.getPurchaseCost()});
            }
        }
    }

    private String getBatchItems(List<Item> batchItems) {
        String s = "";
        for (Item i : batchItems) {
            s += i.getProduct().getName() + ", ";
        }

        return s.substring(0, s.length() - 2);
    }

    public void createNewStocksController() {
        NewStocksController newStocksController = new NewStocksController(this);
        newStocksController.displayWindow();
    }

    public void createNewStocksControllerCallback(Batch batch, List<Item> items) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();

        model.insertRow(model.getRowCount(), new Object[]{batch.getNumber(),
                    DateUtil.formatDate(batch.getDateReceived()),
                    batch.getReceivedBy().getName(),
                    getBatchItems(items),
                    batch.getPurchaseCost()});

        parentController.getInventoryTabController().refreshTable();
    }

    public void createViewBatchController(String batchNumber) {
        ViewBatchController controller = new ViewBatchController(this, batchNumber);
        controller.displayWindow();
    }

    public void clearTable() {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0);
    }

    public void refreshTable() {
        clearTable();
        populateStocksArrivalTable();
    }

    public PaginateSearchAndFilter<Batch> getPsaf() {
        return psaf;
    }

    public void setPsaf(PaginateSearchAndFilter<Batch> psaf) {
        this.psaf = psaf;
    }

    @Override
    public void bindElements() {
        table.setRowSorter(new CustomTableRowSorter(table.getRowSorter().getModel()) {
            @Override
            public void sort() {
                if (table.isShowing()) {
                    Point p = MouseInfo.getPointerInfo().getLocation();
                    int column = getSelectedColumn(p);
                    if (psaf.isColumnSortable(column)) {
                        populateStocksArrivalTable(psaf.sort(column));
                    }
                }
            }
        });
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                populateStocksArrivalTable(psaf.search());
            }
        });
        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Batch> batches = psaf.nextPage();
                if (batches != null) {
                    populateStocksArrivalTable(batches);
                }
            }
        });

        prevButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Batch> batches = psaf.previousPage();
                if (batches != null) {
                    populateStocksArrivalTable(batches);
                }
            }
        });
    }

    private Integer getSelectedColumn(Point point) {
        int mousex = point.x - table.getLocationOnScreen().x;
        int mousey = point.y - table.getLocationOnScreen().y;
        Point mousep = new Point(mousex, mousey);

        return table.columnAtPoint(mousep);
    }
}
